#include "netclient.h"
#include <QNetWorkReply>
#include <QUuid>

#include "interview.qpb.h"
#include "../model/data.h"
#include "../model/datacenter.h"

namespace network{

void NetClient::ping()
{
    QNetworkRequest req;
    req.setUrl(HTTP_URL + "ping");

    auto rsp = _httpClient.get(req);
    connect(rsp, &QNetworkReply::finished, [rsp]{
        if (rsp->error() != QNetworkReply::NoError) {
            qDebug()<< "http请求失败：" << rsp->errorString();
            rsp->deleteLater();
            return;
        }
        qDebug() << rsp->readAll();
        rsp->deleteLater();
    });
}

QNetworkReply *NetClient::sendHttpRequest(const QString &subPath, const QByteArray &body)
{
    QNetworkRequest req;
    req.setUrl(HTTP_URL + subPath);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-protobuf");

    return _httpClient.post(req, body);
}

void NetClient::searchInterview(const model::SearchLimits &limits)
{
    quan_mian::SearchInterviewReq pbReq;
    pbReq.setRequestId(makeRequestId());
    pbReq.setSearchLimits(limits);

    auto rsp = sendHttpRequest("service/interview/search_interview", pbReq.serialize(&_serializer));
    qDebug() << "发送搜索面经请求";
    connect(rsp, &QNetworkReply::finished, [rsp, this]{
        bool ok = false;
        QString errmsg;
        auto pbRsp = this->getHttpResponse<quan_mian::SearchInterviewRsp>(rsp, &ok, &errmsg);
        if (ok == false) {
            qDebug() << "搜索面经响应失败: " << errmsg;
            return;
        }
        auto& dataCenter = model::DataCenter::getInstance();
        dataCenter.resetSearchInterviewResult(pbRsp);
        emit dataCenter.searchInterviewDone();
        qDebug() << "搜索面经响应成功: " << pbRsp->requestId();
    });
}

void NetClient::searchQuestionCluster(const model::SearchLimits &limits)
{
    quan_mian::SearchQuestionClusterReq pbReq;
    pbReq.setRequestId(makeRequestId());
    pbReq.setSearchLimits(limits);

    auto rsp = sendHttpRequest("service/interview/search_question_cluster", pbReq.serialize(&_serializer));
    qDebug() << "发送搜索问题请求";
    connect(rsp, &QNetworkReply::finished, [rsp, this]{
        bool ok = false;
        QString errmsg;
        auto pbRsp = getHttpResponse<quan_mian::SearchQuestionClusterRsp>(rsp, &ok, &errmsg);
        if (ok == false) {
            qDebug() << "搜索问题响应失败: " << errmsg;
            return;
        }
        auto& dataCenter = model::DataCenter::getInstance();
        dataCenter.resetSearchQuestionClusterResult(pbRsp);
        emit dataCenter.searchQuestionClusterDone();
        qDebug() << "搜索问题响应成功: " << pbRsp->requestId();
    });
}

void NetClient::searchCompany(const model::SearchLimits &limits)
{
    quan_mian::SearchCompanyReq pbReq;
    pbReq.setRequestId(makeRequestId());
    pbReq.setSearchLimits(limits);

    auto rsp = sendHttpRequest("service/interview/search_company", pbReq.serialize(&_serializer));
    qDebug() << "发送搜索公司请求";
    connect(rsp, &QNetworkReply::finished, [rsp, this]{
        bool ok = false;
        QString errmsg;
        auto pbRsp = getHttpResponse<quan_mian::SearchCompanyRsp>(rsp, &ok, &errmsg);
        if (ok == false) {
            qDebug() << "搜索公司响应失败: " << errmsg;
            return;
        }
        auto& dataCenter = model::DataCenter::getInstance();
        dataCenter.resetSearchCompanyResult(pbRsp);
        emit dataCenter.searchCompanyDone();
        qDebug() << "搜索公司响应成功: " << pbRsp->requestId();
    });
}

void NetClient::uploadInterview(const model::InterviewInfo &interview)
{
    quan_mian::UploadInterviewReq pbReq;
    pbReq.setRequestId(makeRequestId());
    pbReq.setInterview(interview);

    auto rsp = sendHttpRequest("service/interview/upload_interview", pbReq.serialize(&_serializer));
    qDebug() << "发送上传面经请求";
    connect(rsp, &QNetworkReply::finished, [rsp, this]{
        bool ok = false;
        QString errmsg;
        auto pbRsp = getHttpResponse<quan_mian::UploadInterviewRsp>(rsp, &ok, &errmsg);
        if (ok == false) {
            qDebug() << "上传面经失败: " << errmsg;
            return;
        }
        auto& dataCenter = model::DataCenter::getInstance();
        emit dataCenter.uploadInterviewDone();
    });
}

QString NetClient::makeRequestId() const
{
    return "request-" + QUuid::createUuid().toString().slice(1, 36);
}

} // namespace network
